package com.drcurd.service.impl;

import com.drcurd.entity.Brand;
import com.drcurd.mapper.BrandMapper;
import com.drcurd.service.BrandService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * 品牌服务实现类
 */
@Service
public class BrandServiceImpl implements BrandService {
    
    @Autowired
    private BrandMapper brandMapper;
    
    @Override
    public PageInfo<Brand> getBrandList(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Brand> list = brandMapper.selectAll();
        return new PageInfo<>(list);
    }
    
    @Override
    public List<Brand> getAllBrands() {
        return brandMapper.selectAll();
    }
    
    @Override
    public Brand getBrandById(Long id) {
        return brandMapper.selectById(id);
    }
    
    @Override
    public boolean addBrand(Brand brand) {
        return brandMapper.insert(brand) > 0;
    }
    
    @Override
    public boolean updateBrand(Brand brand) {
        return brandMapper.update(brand) > 0;
    }
    
    @Override
    public boolean deleteBrand(Long id) {
        return brandMapper.deleteById(id) > 0;
    }
    
    @Override
    public PageInfo<Brand> getBrandListByCondition(com.drcurd.dto.BrandQueryDTO queryDTO) {
        // 计算偏移量
        int offset = (queryDTO.getPageNum() - 1) * queryDTO.getPageSize();
        
        // 设置偏移量和大小
        queryDTO.setOffset(offset);
        queryDTO.setSize(queryDTO.getPageSize());
        
        // 查询数据
        List<Brand> list = brandMapper.selectByCondition(queryDTO);
        
        // 查询总数
        int total = brandMapper.selectCountByCondition(queryDTO);
        
        // 创建PageInfo对象
        PageInfo<Brand> pageInfo = new PageInfo<>(list);
        pageInfo.setTotal(total);
        pageInfo.setPageNum(queryDTO.getPageNum());
        pageInfo.setPageSize(queryDTO.getPageSize());
        
        return pageInfo;
    }
} 